/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.JavaXML.DOMValidator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.Serializer;
import nux.xom.xquery.XQueryUtil;

/**
 *
 * @author thanhtq
 */
public class CreateSurvey {
    public static final String SURVEY_XML = "D:\\Project\\thanhtv00011\\Survey\\Survey.xml";
    public static final String SURVEY_XPATH = "//Survey/categorys/category";

    private Document doc = null;
    public Document create(Survey survey) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(SURVEY_XML));
            Node nodes = doc.query(SURVEY_XPATH).get(0);
            Element content = new Element("content");
            content.appendChild(survey.getContentSurvey());
            Element question = new Element("questions");
            Element answer = new Element("answers");
            Element comment = new Element("comment");
            comment.appendChild(survey.getCommenSurvey());
            Element category = new Element("category");
            Attribute id = new Attribute("id", survey.getIdSurvey());
            category.addAttribute(id);
            category.appendChild(content);
            category.appendChild(question);
            category.appendChild(answer);
            category.appendChild(comment);
            nodes.getParent().fastInsertChild(category, nodes.getParent().getChildCount());
            save();
            return doc;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public void updateAnswer(Answer answer){
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(SURVEY_XML));
            Node nodes = doc.query("//Survey/categorys/category/answers/text()").get(0);

            Element option = new Element("option");
            option.appendChild(answer.getOption());
            Element QuesID = new Element("QuesID");
            QuesID.appendChild(answer.getQuesId());
            Element rate = new Element("rate");
            rate.appendChild("0");
            Element answerElement = new Element("answer");
            Attribute id = new Attribute("id", answer.getId());
            answerElement.addAttribute(id);
            answerElement.appendChild(option);
            answerElement.appendChild(QuesID);
            answerElement.appendChild(rate);
            nodes.getParent().fastInsertChild(answerElement, nodes.getParent().getChildCount());
            save();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Document updateQuestion(Question question) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(SURVEY_XML));
            Node nodes = doc.query("//Survey/categorys/category/questions/text()").get(0);
            Element content = new Element("Content");
            content.appendChild(question.getContent());
            Element questionElement = new Element("question");
            Attribute id = new Attribute("id", question.getId());
            questionElement.addAttribute(id);
            questionElement.appendChild(content);
            nodes.getParent().fastInsertChild(questionElement, nodes.getParent().getChildCount());
            save();
            return doc;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public static void main(String argm[]){

//        Survey s = new Survey();
//        s.setName("thanhtq");
//        s.setCommenSurvey("");
//        s.setContentSurvey("adfdfdfdfdfdf");
//        s.setIdSurvey("3");
//        new CreateSurvey().create(s);
        Question q = new Question();
        q.setContent("question 5");
        q.setId("5");
        new CreateSurvey().updateQuestion(q);

        Answer a = new Answer();
        a.setId("9");
        a.setOption("option 1");
        a.setQuesId("1");
        a.setRate("0");
        new CreateSurvey().updateAnswer(a);
        
    }

    public void update(int rate,String id){

        try {
            Builder builder = new Builder();
            doc = builder.build(new File(SURVEY_XML));
            XQueryUtil.update(doc, "//Survey/categorys/category[@id='1']/questionId/id[@id='2']/@enable",String.valueOf(rate));
            save();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void removeSurvey(String id)
    {
        CreateQuestionAndAnswer create = new CreateQuestionAndAnswer();
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(SURVEY_XML));
            Node node = doc.query(SURVEY_XPATH).get(0);
            create.removeQuestionByCaid(id);
            Node nodeChild = doc.query("//Survey/categorys/category[@id='"+id+"']").get(0);

            //node.getParent().removeChild(nodeChild);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public boolean save() {
        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream(new File(SURVEY_XML));
            Serializer ser = new Serializer(fos);
            ser.write(doc);
            ser.flush();            
            return true;
        }catch(Exception ex){
        return false;
        }finally{
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(CreateSurvey.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
